//
//  LandmarkList.swift
//  Landmarks
//
//  Created by 温哲 on 2021/2/10.
//

import SwiftUI

struct LandmarkList: View {
    @EnvironmentObject var modelData: ModelData

    @State private var showFavoritesOnly = false

    var filteredLandmarks: [Landmark] {
        modelData.landmarks.filter { landmark in
            (!showFavoritesOnly) || landmark.isFavorite
        }
    }

    var body: some View {
        NavigationView { List {
            Toggle(isOn: $showFavoritesOnly, label: {
                Text("Favorites only")
            })
            ForEach(filteredLandmarks) {
                landmark in
                NavigationLink(
                    destination: LandmarkDetail(landmark: landmark)) {
                        LandmarkRow(landmark: landmark)
                }
            }
        }.navigationTitle("Landmarks") }
    }
}

struct LandmarkList_Previews: PreviewProvider {
    static var previews: some View {
//        ForEach(["iPhone 8 Plus", "iPhone 12 Pro Max"], id: \.self) { deviceName in
        LandmarkList().environmentObject(ModelData())
//                .previewDevice(PreviewDevice(rawValue: deviceName))
//        }
    }
}
